package com.cat.chapter038;

import java.util.Stack;

/**
 * @author cat
 * @description
 * @create 2025/7/8 13:40
 * @since JDK17
 */

public class Solution06 {
    static Stack<Integer> stack = new Stack<>();
    public static void reverse() {
        if (stack.isEmpty()) {
            return;
        }
        int ans = bottomOut();
        reverse();
        stack.push(ans);
    }

    public static Integer bottomOut() {
        Integer ans = stack.pop();  // 弹出一个
        if (stack.isEmpty()) {  // 如果栈里面没东西了
            return ans;
        } else {
            Integer last = bottomOut();
            stack.push(ans);
            return last;
        }
    }

    public static void main(String[] args) {
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);

        reverse();

        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
}
